package bankclientapplication;

import java.io.*;
import java.net.*;

public class BankClient {
  static int port = 5432;
  static String host = "127.0.0.1";
  Socket requestSocket;
  BufferedWriter out;
  BufferedReader in;
  BufferedReader userinput;
  String command;
  
  private void run() throws IOException {
    try{
      try{
        requestSocket = new Socket(host,port);
      }
      catch(UnknownHostException e){
        System.err.println("Неизвестный хост : " + host);
        return;
      }
      catch(IOException e){
        System.err.println("Не могу подключиться к удалённому серверу; host : " + 
            host + " port: " + port);
        System.err.println(e.getMessage());
        return;
      }
      System.out.println("Соединение с хостом " + host + " установлено.");
      try{
        out = new BufferedWriter(new OutputStreamWriter(requestSocket.getOutputStream()));
        in = new BufferedReader(new InputStreamReader(requestSocket.getInputStream()));
        userinput = new BufferedReader(new InputStreamReader(System.in));
      }
      catch(IOException e){
        e.printStackTrace();
        return;
      }
      do{
        command = userinput.readLine();
        out.write(command);
        System.out.println(in.read());
      }while(!command.equals("quit"));
    }
    finally{
      try{
        requestSocket.close();
        userinput.close();
        out.close();
        in.close();
      }
      catch(IOException e){
        e.printStackTrace();
      }
    }
  }
  
  
  public static void main(String[] args) throws IOException{
    BankClient client = new BankClient();
    client.run();
  }
}
